package myself.a002StackAndQueue;

import java.util.Stack;

/**
 * @author 叶之越
 * Description
 * Date 2021/1/21
 * Time 22:17
 * Mail 739153436@qq.com
 */
public class TwoStacksImplementQueue {
    private Stack<Integer> in = new Stack<>();
    private Stack<Integer> out = new Stack<>();

    public static void main(String[] args) throws Exception {
        TwoStacksImplementQueue queue = new TwoStacksImplementQueue();
        queue.push(1);
        queue.push(2);
        queue.pop();
        queue.push(3);
        while (!queue.isEmpty()) {
            System.out.println(queue.pop());
        }
    }

    public void push(int x) {
        in.push(x);
    }

    public Integer pop() throws Exception {
        if (isEmpty()) {
            throw new Exception();
        }
        if (out.isEmpty()) {
            while (!in.isEmpty()) {
                out.push(in.pop());
            }
        }
        return out.pop();
    }

    public boolean isEmpty() {
        return out.isEmpty() && in.isEmpty();
    }
}
